import requests
from pathlib import Path

DEBUG = False
URL = "http://localhost:8085"
if not DEBUG:
    URL = "http://118.178.94.244"

# 下载相应的xls文件
def Download(stockID: str, date: str, saveDir: Path):
    # print(saveDir / f"cjmx_{date}_{stockID}.xls")
    response = requests.get(f"{URL}/cjmx?id={stockID}&time={date}")
    # print(response)
    if response.ok:
        open(saveDir / f"cjmx_{date}_{stockID}.xls", "wb").write(response.content)
    else:
        print(response.content)

# 更新某个股票的文件列表到服务器
def UpdateStockData(stockID: str):
    response = requests.get(f"{URL}/cjmx?id={stockID}")
    if response.ok:
        print("Updating...")

# 查看某个股票已经下载的文件列表
def ListStockByID(stockID: str):
    response = requests.get(f"{URL}/cjmx/list/{stockID}")
    if response.ok:
        print(response.content)


# 使用示例
if __name__ == "__main__":
    stockID = "002512"  # 股票代码
    date = "20230103"  # 日期
    saveDir = Path("./xls")  # 保存到的目录
    saveDir.mkdir(exist_ok=True)

    # UpdateStockData(stockID)  # 获取最新更新
    # ListStockByID(stockID) # 查看已经获取的对应股票
    # Download(stockID, date, saveDir) # 下载